import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginView.vue')
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 导航守卫         
// 使用 router.beforeEach(进入路由之前) 注册一个全局前置守卫，判断用户是否登陆 
//next():用于控制路由跳转的行为
// router.beforeEach((to, from, next) => {
//   if (to.path === '/Login') {
//     console.log('当前为登陆页面');
//     next();
//   }else{
//     let token = sessionStorage.getItem('token');
//     if (token == null || token == '') {
//       console.log('未登录');
//       next('/Login');
//     }else {
//       console.log('已登录');
//       next();
//     }
//   }
// })

export default router
